package cn.net.isir.controller.miniprogram;

import cn.net.isir.common.RequestObject;
import cn.net.isir.utils.RequestUtils;
import cn.net.isir.common.ResultObject;
import cn.net.isir.entity.Shop;
import cn.net.isir.entity.Table;
import cn.net.isir.service2.ShopService;
import cn.net.isir.service2.TableService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuxubo
 * @filename ShopController.java
 * @package cn.net.isir.controller.miniprogram
 * @email lxb@isir.net.cn
 * @date 2018/11/12
 * @comment
 * @since 1.8
 */
@RestController("minipShopController")
@RequestMapping(value = "/minip/shop")
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = {Exception.class, RuntimeException.class})
public class ShopController {

    @Resource
    private ShopService shopService;
    @Resource
    private TableService tableService;

    @RequestMapping(method = RequestMethod.GET, value = "/baseinfo")
    public ResultObject getBaseInfo(RequestObject obj, HttpServletRequest request, HttpServletResponse response) {
        //声明返回值
        ResultObject ro;
        //声明参数解码后的储存变量
        Map<String, Object> reqParamMap = new HashMap<>();
        //需要检查的字段名
        String[] columns = new String[]{"shopid"};
        //检查失败则返回缺失的字段提示
        if ((ro = RequestUtils.parametersCheck(obj, reqParamMap, columns)).getResult() != 200) {
            return ro;
        }
        String shopid = String.valueOf(reqParamMap.get("shopid"));
        Shop shop = new Shop();
        shop.setId(shopid);
        shop = shopService.selectOne(shop);
        ro.setResult(200);
        ro.setMessage("success");
        ro.setObject(shop);
        return ro;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/tables")
    public ResultObject getTables(RequestObject obj, HttpServletRequest request, HttpServletResponse response) {
        //声明返回值
        ResultObject ro;
        //声明参数解码后的储存变量
        Map<String, Object> reqParamMap = new HashMap<>();
        //需要检查的字段名
        String[] columns = new String[]{"shopid"};
        //检查失败则返回缺失的字段提示
        if ((ro = RequestUtils.parametersCheck(obj, reqParamMap, columns)).getResult() != 200) {
            return ro;
        }
        String shopid = String.valueOf(reqParamMap.get("shopid"));
        Table table = new Table();
        table.setShopId(shopid);
        List<Table> tableList = tableService.selectList(table);
        ro.setResult(200);
        ro.setMessage("success");
        ro.setObject(tableList);
        return ro;
    }
}
